// 租户信息管理mock数据
export const ecsSubscriberInfoMockData = {
  // 证件类型枚举
  cardTypes: [
    { paramKey: '1', paramValue: '身份证' },
    { paramKey: '2', paramValue: '护照' },
    { paramKey: '3', paramValue: '军官证' },
    { paramKey: '4', paramValue: '驾驶证' }
  ],

  // 租户信息列表
  subscriberList: Array.from({ length: 50 }, (_, i) => ({
    subscriberId: i + 1,
    subscriberName: `租户${i + 1}`,
    subscriberAddress: `地址${i + 1}号`,
    phone: `1836133${(6336 + i).toString().slice(-4)}`,
    uname: `U${(1000 + i).toString()}`,
    certificateType: ((i % 4) + 1).toString(),
    certificateValue: `${110101}${(19900101 + i * 100).toString().slice(-8)}${(1000 + i).toString().slice(-3)}X`,
    subscriberType: i % 2,
    createTime: new Date(2025, 0, 1 + i % 30, 10 + i % 12, i % 60).toLocaleString('zh-CN')
  }))
}

// Mock API函数
export const ecsSubscriberInfoApi = {
  // 获取证件类型列表
  getCardTypes() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 0,
          msg: 'success',
          page: ecsSubscriberInfoMockData.cardTypes
        })
      }, 200)
    })
  },

  // 获取租户信息列表
  getSubscriberList(params = {}) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const { page = 1, limit = 10, paramKey = '' } = params
        let filteredData = ecsSubscriberInfoMockData.subscriberList

        // 根据姓名搜索
        if (paramKey) {
          filteredData = filteredData.filter(item => 
            item.subscriberName.includes(paramKey)
          )
        }

        const start = (page - 1) * limit
        const end = start + limit
        const list = filteredData.slice(start, end)

        resolve({
          code: 0,
          msg: 'success',
          page: {
            list: list,
            totalCount: filteredData.length,
            pageSize: limit,
            currPage: page
          }
        })
      }, 300)
    })
  },

  // 获取租户详情
  getSubscriberInfo(id) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const subscriber = ecsSubscriberInfoMockData.subscriberList.find(
          item => item.subscriberId === parseInt(id)
        )
        if (subscriber) {
          resolve({
            code: 0,
            msg: 'success',
            data: subscriber
          })
        } else {
          resolve({
            code: 500,
            msg: '租户不存在'
          })
        }
      }, 200)
    })
  },

  // 保存租户信息
  saveSubscriber(data) {
    return new Promise((resolve) => {
      setTimeout(() => {
        if (data.subscriberId) {
          // 更新
          const index = ecsSubscriberInfoMockData.subscriberList.findIndex(
            item => item.subscriberId === data.subscriberId
          )
          if (index !== -1) {
            ecsSubscriberInfoMockData.subscriberList[index] = {
              ...ecsSubscriberInfoMockData.subscriberList[index],
              ...data
            }
          }
        } else {
          // 新增
          const newId = Math.max(...ecsSubscriberInfoMockData.subscriberList.map(s => s.subscriberId)) + 1
          const newSubscriber = {
            ...data,
            subscriberId: newId,
            createTime: new Date().toLocaleString('zh-CN')
          }
          ecsSubscriberInfoMockData.subscriberList.push(newSubscriber)
        }

        resolve({
          code: 0,
          msg: '操作成功'
        })
      }, 300)
    })
  },

  // 删除租户
  deleteSubscriber(ids) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const idsArray = Array.isArray(ids) ? ids : [ids]
        ecsSubscriberInfoMockData.subscriberList = ecsSubscriberInfoMockData.subscriberList.filter(
          item => !idsArray.includes(item.subscriberId)
        )

        resolve({
          code: 0,
          msg: '删除成功'
        })
      }, 300)
    })
  }
}

export default {
  ecsSubscriberInfoMockData,
  ecsSubscriberInfoApi
}

